package com.example.service;

import cn.hutool.core.collection.CollUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Report;
import com.example.exception.CustomException;
import com.example.mapper.ReportMapper;
import com.example.utils.TimeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReportService {
    @Resource
    ReportMapper reportMapper;

    public void set(Report report) {
        report.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        List<Report> reportList = reportMapper.selectReport(report);
        if (CollUtil.isNotEmpty(reportList)) {
            throw new CustomException(ResultCodeEnum.REPORT_EXIST);
        } else {
            reportMapper.insert(report);
        }
    }

    public List<Report> selectReport(Integer fid,Integer userId, String module) {
        Report report = new Report();
        report.setUserId(userId);
        report.setFid(fid);
        report.setModule(module);
        return reportMapper.selectReport(report);
    }

    public List<Report> selectAll(Report report) {
        List<Report> list = reportMapper.selectAll(report);
        list.forEach(entity-> entity.setReleaseTime(TimeUtils.TimestampToDateFormat(entity.getCreateTime())));
        return list;
    }

    public PageInfo<Report> selectPage(Report report, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Report>list=reportMapper.selectAll(report);
        list.forEach(entity-> entity.setReleaseTime(TimeUtils.TimestampToDateFormat(entity.getCreateTime())));
        return PageInfo.of(list);
    }
}
